package com.info3k.server.domain;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.info3k.client.domain.Address;
import com.info3k.client.domain.Location;
import com.info3k.client.utils.ToolsClient;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class LocationP implements Serializable {

	private static final long serialVersionUID = -1873799364997737422L;

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
    
    @Persistent
	private String locationName;
    
    @Persistent
	private Text description;
    
    @Persistent
	private Long floor;
    
    @Persistent
    private List<String> latLong;
    
    @Persistent
    private Set<Key> addresses;
    
    @Persistent
    private Key user;
    
    @Persistent
    private Key category;
    
    @Persistent
    private Double rate;
    
    @Persistent
    private Long views;
    
    @Persistent
    private Long numRates;
    
    public Set<Key> getAddresses() {
		return addresses;
	}

	public void setAddresses(Set<Key> addresses) {
		this.addresses = addresses;
	}
    
    public LocationP() {
    	
    }
    
    public LocationP(Location loc) {
    	this.locationName = loc.getLocationName();
    	
    	this.setDescription(new Text(loc.getDescription()));
    	this.floor = loc.getFloor();
    	
    	DecimalFormat twoDForm = new DecimalFormat("#.##");
    	this.setRate(Double.valueOf(twoDForm.format(loc.getRate())));
    	this.setViews(loc.getViews());
    	this.setNumRates(loc.getNumRates());
    	if(loc.getId()!=null)
    		this.id = KeyFactory.createKey(LocationP.class.getSimpleName(), loc.getId());
    	else
    		this.id = KeyFactory.createKey(LocationP.class.getSimpleName(), System.currentTimeMillis());
		
    	this.latLong = new ArrayList<String>();
    	if(loc.getLatLong()!=null){
    		for (int i = 0; i < loc.getLatLong().length; i++) {
				this.latLong.add(loc.getLatLong()[i]);
			}
    	}
    	this.addresses = new HashSet<Key>();
    	if(loc.getAddresses()!=null){
	    	for (Address address : loc.getAddresses()) {
				Key k = KeyFactory.createKey(AddressP.class.getSimpleName(), address.getId());
				this.addresses.add(k);
			}
    	}
    	this.setUser(KeyFactory.createKey(UserP.class.getSimpleName(), loc.getUser().getId()));
    	
    	if(loc.getCategory()!=null)
    		this.setCategory(KeyFactory.createKey(CategoryP.class.getSimpleName(), loc.getCategory().getId()));
    }
    
    public static Location createLocation(LocationP locationP){
		Location loc = new Location();
		loc.setId(locationP.getId().getId());
		loc.setLocationName(locationP.getLocationName());
		loc.setFloor(locationP.getFloor());
		loc.setDescription(locationP.getDescription().getValue());
		loc.setNumRates(locationP.getNumRates());
		loc.setRate(locationP.getRate());
		loc.setViews(locationP.getViews());
		
		if(locationP.getLatLong()!=null)
			loc.setLatLong(ToolsClient.fromCollectionToArray(locationP.getLatLong(), new String[locationP.getLatLong().size()]));
		
		loc.setAddresses(new Address[0]);
		
		return loc;
    }

	public void setLocationName(String locationName) {
		this.locationName = locationName;
	}

	public String getLocationName() {
		return locationName;
	}

	public void setFloor(Long floor) {
		this.floor = floor;
	}

	public Long getFloor() {
		return floor;
	}

	public void setLatLong(List<String> latLong) {
		this.latLong = latLong;
	}

	public List<String> getLatLong() {
		return latLong;
	}

	public void setId(Key id) {
		this.id = id;
	}

	public Key getId() {
		return id;
	}

	public void setUser(Key user) {
		this.user = user;
	}

	public Key getUser() {
		return user;
	}

	public void setCategory(Key category) {
		this.category = category;
	}

	public Key getCategory() {
		return category;
	}

	public void setRate(Double rate) {
		this.rate = rate;
	}

	public Double getRate() {
		return rate;
	}

	public void setViews(Long views) {
		this.views = views;
	}

	public Long getViews() {
		return views;
	}

	public void setNumRates(Long numRates) {
		this.numRates = numRates;
	}

	public Long getNumRates() {
		return numRates;
	}

	public void setDescription(Text description) {
		this.description = description;
	}

	public Text getDescription() {
		return description;
	}
}
